﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express.Setup.Partner
{
    public partial class CustomerListView : FormBase
    {
        #region Private Variables

        bool _isVisible;

        #endregion

        #region Private Methods

        private void CustomerListView_Load(object sender, EventArgs e)
        {
            base.LoadData();
        }

        private void CustomerListView_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible && !_isVisible)
                base.LoadData();
            _isVisible = this.Visible;
        }

        private void txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            base.LoadData();
        }

        private void txtCustomerCode_TextChanged(object sender, EventArgs e)
        {
            base.LoadData();
        }

        protected override void btnViewAll_Click(object sender, EventArgs e)
        {
            try
            {
                txtCustomerCode.TextChanged -= txtCustomerCode_TextChanged;
                txtCustomerName.TextChanged -= txtCustomerName_TextChanged;

                base.btnViewAll_Click(sender, e);
            }
            finally
            {
                txtCustomerCode.TextChanged += txtCustomerCode_TextChanged;
                txtCustomerName.TextChanged += txtCustomerName_TextChanged;
            }
        }

        #endregion

        #region Constructors

        public CustomerListView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.Customer;

            base.OrderBy = "PartnerCode";
            base.OrderDirection = "ASC";

            base.Action = ActionName.List;
            base.InitializeDataGrid(dtgCustomer);
            base._presenter = new PresenterBase(this, new CustomerENT());

            btnViewAll.Click += btnViewAll_Click;
            dtgCustomer.ColumnHeaderMouseClick += dtg_ColumnHeaderMouseClick;

            Global.SetDoubleBuffered(dtgCustomer);
        }

        #endregion

        #region Public Properties

        [Search]
        public string PartnerCode
        {
            get { return txtCustomerCode.Text; }
            set { txtCustomerCode.Text = value; }
        }

        [Search]
        public string PartnerName
        {
            get { return txtCustomerName.Text; }
            set { txtCustomerName.Text = value; }
        }

        [Search]
        public bool IsCustomer
        {
            get { return true; }
        }

        #endregion
    }
}
